package com.daily.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.daily.entity.Ectur;
import com.daily.entity.Hobbys;
import com.daily.entity.Rang;
import com.daily.entity.Students;
import com.daily.service.StudentsService;

@Controller
public class StudentsController {

  @Autowired
  private StudentsService studentsService;

  //列表方法
  @RequestMapping("getlist")
  public String getlist(Model model) {
    List<Students> stus = studentsService.getlist();
    model.addAttribute("stus", stus);
    return "slist";
  }

  //去修改页面
  @RequestMapping("goxiugai")
  public String goxiugai(int sid, Model model) {
    Students stu = studentsService.getstu(sid);
    model.addAttribute("stu", stu);
    model.addAttribute("sid", sid);
    return "xiugai";
  }

  //去修改页面js方法
  @RequestMapping("goxiugaijs")
  public String goxiugaijs(int sid, Model model) {
    Students stu = studentsService.getstu(sid);
    model.addAttribute("stu", stu);
    model.addAttribute("sid", sid);
    return "xiugaijs";
  }

  //去修改页面JAVA方法
  @RequestMapping("goxiugaiJAVA")
  public String goxiugaiJAVA(int sid, Model model) {
    Students stu = studentsService.getstu(sid);
    List<Hobbys> hob = studentsService.selecthobby();
    List<Ectur> ect = studentsService.selectectur();
    List<Rang> ran = studentsService.selectrang();
    String[] sp = stu.getHobby().split(",");
    List<String> hobby = Arrays.asList(sp);//通过此方法将切割好的爱好数组放入集合中
    String[] ec = stu.getEctur().split(",");
    List<String> ectur = Arrays.asList(ec);//通过此方法将切割好的部门数组放入集合中
    String[] ra = stu.getRang().split(",");
    List<String> rang = Arrays.asList(ra);//通过此方法将切割好的职位数组放入集合中
    //爱好判断回显
    for (int i = 0; i < hob.size(); i++) {
      Hobbys hb = hob.get(i);
      boolean isCheck = false;//根据此属性进行前台复选框的选中
      for (String h : hobby) {
        if (h.equals(hb.getHname())) {
          isCheck = true;
        }
      }
      hb.setCheck(isCheck);
    }
    //部门判断回显
    for (int i = 0; i < ect.size(); i++) {
      Ectur hb = ect.get(i);
      boolean isCheck = false;
      for (String e : ectur) {
        if (e.equals(hb.getEname())) {
          isCheck = true;
        }
      }
      hb.setCheck(isCheck);
    }
    //职位判断回显
    for (int i = 0; i < ran.size(); i++) {
      Rang hb = ran.get(i);
      boolean isCheck = false;
      for (String r : rang) {
        if (r.equals(hb.getRname())) {
          isCheck = true;
        }
      }
      hb.setCheck(isCheck);
    }

    model.addAttribute("hob", hob);
    model.addAttribute("ect", ect);
    model.addAttribute("ran", ran);
    model.addAttribute("stu", stu);
    model.addAttribute("sid", sid);
    return "xiugaiJAVA";
  }

  //去修改页面JSP方法
  @RequestMapping("goxiugaiJSP")
  public String goxiugaiJSP(int sid, Model model) {
    HashMap<String, Object> map = new HashMap<String, Object>();
    Students stu = studentsService.getstu(sid);
    List<Hobbys> hob = studentsService.selecthobby();
    List<Ectur> ect = studentsService.selectectur();
    List<Rang> ran = studentsService.selectrang();
    model.addAttribute("stu", stu);
    model.addAttribute("sid", sid);
    model.addAttribute("hob", hob);
    model.addAttribute("ect", ect);
    model.addAttribute("ran", ran);
    return "xiugaiJSP";
  }

  //查询爱好追加到页面
  @RequestMapping("selecthobby")
  @ResponseBody
  public List<Hobbys> selecthobby() {
    List<Hobbys> hob = studentsService.selecthobby();
    return hob;
  }

  //根据id获取对象
  @RequestMapping("selecthobbybyid")
  @ResponseBody
  public Students selecthobbybyid(int sid) {
    Students stu1 = studentsService.getstu(sid);
    return stu1;
  }

  //查询部门追加到页面
  @RequestMapping("selectectur")
  @ResponseBody
  public List<Ectur> selectectur() {
    List<Ectur> ect = studentsService.selectectur();
    return ect;
  }

  //查询职位追加到页面
  @RequestMapping("selectrang")
  @ResponseBody
  public List<Rang> selectrang() {
    List<Rang> ran = studentsService.selectrang();
    return ran;
  }

  //删除与批量删除
  @RequestMapping("deletebyids")
  @ResponseBody
  public boolean deletebyids(String ids) {
    boolean a = false;
    String[] s = ids.split(",");
    for (String sid : s) {
      a = studentsService.deletebyids(sid);
    }
    return a;
  }

  //跳转添加页面
  @RequestMapping("goaddstudents")
  public String goaddstudents() {
    return "tianjia";
  }

  //添加方法
  @RequestMapping("addstudent")
  @ResponseBody
  public boolean addstudent(Students stu) {
    boolean a = studentsService.addstudent(stu);
    return a;
  }

  //修改方法
  @RequestMapping("updatestudent")
  @ResponseBody
  public boolean updatestudent(Students stu) {
    boolean b = studentsService.updatestudent(stu);
    return b;
  }

}
